<template>
  <el-dialog title="派单" :visible.sync="params.editShow">
    <el-form id="fromUser" @submit.native.prevent="submit" :model="res" ref="from" :rules="rules" >
      <el-form-item label="请选择要派单的检测师" required prop="userId" >
        <el-select placeholder="请选择检测师" v-model="res.userId" >
          <el-option v-for="(item, index) in userOptions" :key="index" :label="item.userName" :value="item.userId"></el-option>
        </el-select>
        <input type="hidden" name="detectUserId" :value="res.userId" >
        <input type="hidden" name="detectionId" :value="params.obj.detectionId" >
        <input type="hidden" name="marketId" :value="$sess.get('userInfo').marketId" >
        <input type="hidden" name="detectionState" value="10" >
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="params.editShow = false">取 消</el-button>
      <button class="el-button el-button--primary" form="fromUser" >确 定</button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    props: [
      'params'
    ],
    data () {
      return {
        res: {
          userId:''
        },
        userOptions: [],
        rules:{
          userId:[
            {
              required: true,
              message: "请选择检测师"
            }
          ]
        }
      }
    },
    created () {
      this.$axios.get(this.$api.getUserList, {params: {marketId: this.$sess.get('userInfo').userId, userType: 32}}).then(res => {
        res && res.data.result && (this.userOptions = res.data.result)
      })
    },
    methods: {
      submit (event) {
        this.$refs['from'].validate(bool => {
          if (bool) {
            // let fd = new FormData(event.target)
            var URL = this.params.editUser ? this.$api.changeDetectUserId : this.$api.updateDetection
            
            this.$axios.get(URL, {params: {
              carId: this.params.obj.car.carId,
              detectUserId: this.res.userId,
              detectionId: this.params.obj.detectionId,
              // marketId: this.params.marketId,
              detectionState: 10
            }}).then(res => {
              res && res.data.code == 1 && (this.$message({type: 'success', message: '派单成功'}) + (this.$emit('reset'))) 
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>
